<template>
  <div>
    <div class="header">
      <div><i class="iconfont icon-yonghu"></i>广西</div>
      <div>医疗救治定点医院和发热门诊一览</div>
    </div>
    <div class="content">
      <div class="sous">
        <span style="color:#ccc">搜索：</span>
        <el-input v-model="obj.name"  placeholder="请输入医院名称"></el-input>
      </div>
      <div class="fenbu">
        <el-button @click='tohosFenbu(hos.list)'>分 布</el-button>
      </div>
      <div
        class="item"
        v-for="item in hos.list"
        :key="item.id"
      >
        <div @click="toMap(item)" class="left">
          <div>{{ item.name }}</div>
          <div v-if="item.flags">{{ item.flags }}</div>
          <div v-else>暂无标记</div>
          <div v-if="item.address">{{ item.address }}</div>
          <div v-else>地址不详</div>
        </div>
        <div @click="toHos(item)" class="right">
          <div><i class="iconfont icon-daohang"></i></div>
          <div>{{GetDistance(item.longitude,item.latitude)}}Km</div>  <!-- 调用GetDistance方法 -->
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
export default {
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true,
    },
  },
  data() {
    return {
      obj: {
        page: 1,
        pageSize: 100,
      },
      lat: 0,
      lng: 0,
    };
  },
  computed: {
    ...mapState("hos", ["hos"]),
  },
  created() {
    this.findHos(this.obj);
    this.getLocation();
  },
  methods: {
    ...mapActions("hos", ["findHos"]),
    // 方法定义 lat,lng
    // 计算距离
    GetDistance(lng1, lat1) {
      var radLat1 = (lat1 * Math.PI) / 180.0;
      var radLat2 = this.lat * Math.PI / 180.0;
      var a = radLat1 - radLat2;
      var b = (lng1 * Math.PI) / 180.0 - this.lng * Math.PI / 180.0;
      var s =
        2 *
        Math.asin(
          Math.sqrt(
            Math.pow(Math.sin(a / 2), 2) +
              Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)
          )
        );
      s = s * 6378.137; // EARTH_RADIUS;
      s = Math.round(s * 10000) / 10000;
      return s;
    },

    getLocation() {
      // var options = {
      //   enableHighAccuracy: true,
      //   timeout: 5000,
      //   maximumAge: 0,
      // };

      // function success(pos) {
      //   var crd = pos.coords;

      //   console.log("Your current position is:");
      //   console.log("Latitude : " + crd.latitude);
      //   console.log("Longitude: " + crd.longitude);
      //   console.log("More or less " + crd.accuracy + " meters.");
      // }

      // function error(err) {
      //   console.warn("ERROR(" + err.code + "): " + err.message);
      // }

      navigator.geolocation.getCurrentPosition(
        (res) => {
          this.lng = res.coords.longitude;
          this.lat = res.coords.latitude;
        },
        (err) => {
          console.log(err);
        },
        { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }
      );
    },
    toMap(val) {
      // 页面跳转,跳转至疫情地图页面，并携带参数（经纬度）
      this.$router.push({
        // path:'/location/index',
        path: "/hos/detail",
        query: {
          one: val.latitude,
          two: val.longitude,
        },
      });
    },
     toHos(val){
            this.$router.push({
                path:'/hos/detail01',
                query:{
                    one:val.longitude,
                    two:val.latitude,
                },
            })
        },
     tohosFenbu(val){
            this.$router.push({
                path:'/hos/detail03',
                query:{
                    one:val.longitude,
                    two:val,
                },
            })
        },
  },
};
</script>
<style scoped>
.icon-daohang {
  text-align: center;
  font-size: 23px;
  color:#0f89f5;
  right: 50px;
}
.content .sous {
  width: 180px;
}
.content .fenbu {
  float: right;
  margin-right: 30px;
}
.content .right div:first-child {
  float: right;
  margin-right: 30px;
  /* padding-left: 10px; */
}
.content .left div:nth-child(2) {
  background-color: #ffb34e;
  border-radius: 5px;
  font-size: 12px;
  display: inline;
  padding: 5px;
  color: white;
}
.content .left div:last-child {
  font-size: 14px;
  color: #999;
}
.content .left div {
  height: 30px;
  line-height: 30px;
}
.content .right div {
  height: 45px;
  line-height: 45px;
}
.content .item {
  margin-top: 40px;
  border-bottom: 1px solid #ccc;
  height: 90px;
  padding: 0 20px;
}
.content .left {
  float: left;
}
.content .right {
  float: right;
}
.content {
  margin-top: 160px;
}
.header div:last-child {
  top: 55%;
  left: 3%;
}
.header div:first-child {
  top: 25%;
  left: 3%;
}
.header div {
  position: absolute;
  font-size: 20px;
  color: white;
  font-weight: 800px;
}
.header {
  height: 150px;
  background-image: url("http://39.96.21.48/2020_znn/visual-ui/static/img/bg1.efce26ef.png");
  background-size: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
}
</style>